<?php 
class yingClassAction extends ActionNot{



	
	public function initAction()
	{

		$this->mweblogin(0, true);

	}
	
	public function defaultAction()
	{
		$num = $this->get('num');
		$arr = m('reim')->getagent(0, "and `num`='$num'");
		if(!$arr)exit('sorry not found agent['.$num.']!');
		$rs  = $arr[0];
		$this->title = $rs['name'];
		$yyurl 	= ''.P.'/we/ying/yingyong/'.$num.'.html';

		$xxdataArr = m('kqxxsj')->getall("",'*','`id` desc');


		$xxArr = '('."'";

		foreach($xxdataArr as $key=>$val) {

			if($key == count($xxdataArr)-1){
				$xxArr= $xxArr.$val['dt']."'";
			}else{
				$xxArr = $xxArr.$val['dt']."'".','."'";
			}
		}
		$xxArr = $xxArr.')';

        $dataArr = m('location')->getall("uid='$this->adminid'and !(LEFT(optdt,10) in $xxArr) and type='0' GROUP BY LEFT(optdt,10) ",'MAX(optdt)as `max`,MIN(optdt)as  `min` ,label as `label` ','`id` desc');


		if(!file_exists($yyurl))$yyurl='';
		$yyurljs 	= ''.P.'/we/ying/yingyong/'.$num.'.js';
		if(!file_exists($yyurljs))$yyurljs='';
		$this->assign('arr', $rs);
        $this->assign('dataArr', $dataArr);
        $this->assign('xxdataArr', $xxdataArr);
		$this->assign('openfrom', $this->get('openfrom'));
		$this->assign('yyurl', $yyurl);
		$this->assign('yyurljs', $yyurljs);
	}

    public function customerAction()
    {
        $num = $this->get('num');
        $arr = m('reim')->getagent(0, "and `num`='$num'");
        if(!$arr)exit('sorry not found agent['.$num.']!');
        $rs  = $arr[0];
        $this->title = $rs['name'];
        $yyurl 	= ''.P.'/we/ying/yingyong/'.$num.'.html';


        $customerArr = m('customer')->getall("",'id,status,isstat','`id` desc');

        $this->assign('customerArr', $customerArr);

        if ( isajax() ){

            $customerArr = m('customer')->getall("",'id,status,isstat','`id` desc');

            $this->returnjson($customerArr);
        }


        if(!file_exists($yyurl))$yyurl='';
        $yyurljs 	= ''.P.'/we/ying/yingyong/'.$num.'.js';
        if(!file_exists($yyurljs))$yyurljs='';
        $this->assign('arr', $rs);

        $this->assign('openfrom', $this->get('openfrom'));
        $this->assign('yyurl', $yyurl);
        $this->assign('yyurljs', $yyurljs);
    }

    public function default1Action()
    {

         if(isajax()){

             $page = $_POST["page"];
             $pagesize = 10;
             $limit= $page*$pagesize-$pagesize;
             $a=array();

             for ($x= $limit; $x<$page*$pagesize; $x++) {

                 $dt = date("Y-m-d",strtotime("-$x day"));
                 array_push($a,$dt);
             }

             $Arr = '('."'";

             foreach($a as $key=>$val) {

                 if($key == count($a)-1){
                     $Arr= $Arr.$val."'";
                 }else{
                     $Arr = $Arr.$val."'".','."'";
                 }
             }
             $Arr = $Arr.')';
             $dataArr = m('location')->getall("uid='$this->adminid' and (LEFT(optdt,10) in $Arr) and type='0' GROUP BY `date` ",'MAX(optdt)as `max`,MIN(optdt)as  `min` , MIN(label) as  `label`,left(optdt,10) as `date` ','`date` desc');
             $this->returnjson($dataArr);

         }

        $xxdataArr = m('kqxxsj')->getall("",'*','`id` desc');
        $lowidArr = m('admin')->getall("superid LIKE '%$this->adminid%'",'*','');


        $this->assign('xxdataArr', $xxdataArr);
        $this->assign('lowidArr', $lowidArr);
        $this->assign('openfrom', $this->get('openfrom'));

    }


    public function lowerkqAction()
    {

        $lowidArr = m('admin')->getall("superid LIKE '%$this->adminid%'",'*','');

        $uidArr = '(';

        foreach($lowidArr as $key=>$val) {

            if($key == count($lowidArr)-1){
                $uidArr = $uidArr.$val['id'];
            }else{
                $uidArr = $uidArr.$val['id'].',';
            }
        }
        $uidArr = $uidArr.')';

        //休息时间数组

        $xxdataArr = m('kqxxsj')->getall("",'*','`id` desc');

        $xxArr = '('."'";

        foreach($xxdataArr as $key=>$val) {

            if($key == count($xxdataArr)-1){
                $xxArr= $xxArr.$val['dt']."'";
            }else{
                $xxArr = $xxArr.$val['dt']."'".','."'";
            }
        }
        $xxArr = $xxArr.')';



        $this->assign('xxdataArr', $xxdataArr);
        $this->assign('lowidArr', $lowidArr);


        if(isajax()){


            $page = $_POST["page"];
            $pagesize = 3;
            $limit= $page*$pagesize-$pagesize;
            $a=array();

            for ($x= $limit; $x<$page*$pagesize; $x++) {

                $dt = date("Y-m-d",strtotime("-$x day"));
                array_push($a,$dt);
            }

            $Arr = '('."'";

            foreach($a as $key=>$val) {

                if($key == count($a)-1){
                    $Arr= $Arr.$val."'";
                }else{
                    $Arr = $Arr.$val."'".','."'";
                }
            }
            $Arr = $Arr.')';

            $lowArr = m('location')->getall("uid in $uidArr and (LEFT(optdt,10) in $Arr) and !(LEFT(optdt,10) in $xxArr) and type='0'  GROUP BY `date`,uid ",'uid,MAX(optdt)as `max`,MIN(optdt)as  `min` , ANY_VALUE(label) as `label`,left(optdt,10) as `date` ','`date` desc,uid asc');

            $this->returnjson($lowArr);

        }

    }


    public function dailyAction()
    {

        $xxdataArr = m('kqxxsj')->getall("",'*','`id` desc');

        //下属id字符串数组

        $uidArr = '(';

        //休息时间字符串数组

        $xxArr = '('."'";

        foreach($xxdataArr as $key=>$val) {

            if($key == count($xxdataArr)-1){
                $xxArr= $xxArr.$val['dt']."'";
            }else{
                $xxArr = $xxArr.$val['dt']."'".','."'";
            }
        }
        $xxArr = $xxArr.')';



        if ( isajax() ){

            $check = $_POST["check"];

            if($check == '1'){

                $Mid = $_POST["mid"];

                $time = date('Y-m-d H:i:s');

                m('flow_log')->insert("`table` = 'check',`mid`='$Mid',`name`='查看',`optdt`='$time',`checkname`='$this->adminname',`checkid`='$this->adminid'");

            }
            else if($check == '2'){

                $page = $_POST["page"];
                $pagesize = 10;
                $limit= $page*$pagesize-$pagesize;
                $a=array();

                for ($x= $limit; $x<$page*$pagesize; $x++) {

                    $dt = date("Y-m-d",strtotime("-$x day"));
                    array_push($a,$dt);
                }

                $Arr = '('."'";

                foreach($a as $key=>$val) {

                    if($key == count($a)-1){
                        $Arr= $Arr.$val."'";
                    }else{
                        $Arr = $Arr.$val."'".','."'";
                    }
                }
                $Arr = $Arr.')';

                $dailyArr = m('daily')->getall("uid='$this->adminid' and (LEFT(optdt,10) in $Arr)  and !(LEFT(optdt,10) in $xxArr)  ",'id,uid,dt,optdt,content ','id desc,uid asc');

                $this->returnjson($dailyArr);


            }

            else{


                $dailyArr = m('daily')->getall("uid='$this->adminid' and optdt > '2017-08-01' and !(LEFT(optdt,10) in $xxArr)  ",'id,uid,dt,optdt,content ','id desc,uid asc');

                $this->returnjson($dailyArr);
            }


        }

        else{


            $commentArr =  m('flow_log')->getall("`table`='daily' and optdt > '2017-08-01'and !(left(optdt,10) in $xxArr) AND !ISNULL(`explain`) ",'`explain`,optdt ,mid,checkname,checkid  ','optdt desc');

            $lowdailyArr = m('daily')->getall("uid in $uidArr  and optdt > '2017-08-01'and !(LEFT(optdt,10) in $xxArr)   GROUP BY dt,uid ",'id,uid,optdt,content,optname,dt ','`dt` desc,uid asc');

            $lowidArr = m('admin')->getall("superid LIKE '%$this->adminid%'",'*','');

            $this->assign('lowidArr', $lowidArr);
            $this->assign('commentArr', $commentArr);
            $this->assign('xxdataArr', $xxdataArr);
            $this->assign('lowdailyArr', $lowdailyArr);
            $this->assign('openfrom', $this->get('openfrom'));

        }

    }


    public function detailAction()
    {
        if ( isajax()){

            header('Content-type: application/json');

            $xxdataArr = m('kqxxsj')->getall("",'*','`id` desc');

            $xxArr = '('."'";

            foreach($xxdataArr as $key=>$val) {

                if($key == count($xxdataArr)-1){
                    $xxArr= $xxArr.$val['dt']."'";
                }else{
                    $xxArr = $xxArr.$val['dt']."'".','."'";
                }
            }
            $xxArr = $xxArr.')';

            $clickedit = $_POST["clickedit"];

            if ($clickedit == '1'){

                $dailyArr = m('daily')->getall("uid='$this->adminid' and optdt > '2017-08-01' and !(LEFT(optdt,10) in $xxArr)  ",'id,uid,dt,optdt,content ','id desc,uid asc');

                $this->returnjson($dailyArr);


            }else{

                $commentArr =  m('flow_log')->getall("`table`='daily' and optdt > '2017-08-01'and !(left(optdt,10) in $xxArr) AND !ISNULL(`explain`) ",'`explain`,optdt ,mid,checkname,checkid ','optdt desc');

                $this->returnjson($commentArr);
            }


        } else{
            $this->loadDetail();
        }


    }

    function loadDetail(){

        $num = $this->get('num');
        $arr = m('reim')->getagent(0, "and `num`='$num'");
        if(!$arr)exit('sorry not found agent['.$num.']!');
        $rs  = $arr[0];
        $this->title = $rs['name'];
        $yyurl 	= ''.P.'/we/ying/yingyong/'.$num.'.html';


        $xxdataArr = m('kqxxsj')->getall("",'*','`id` desc');

        $lowidArr = m('admin')->getall("superid LIKE '%$this->adminid%'",'*','');


        //下属id字符串数组

        $uidArr = '(';

        foreach($lowidArr as $key=>$val) {

            if($key == count($lowidArr)-1){
                $uidArr = $uidArr.$val['id'];
            }else{
                $uidArr = $uidArr.$val['id'].',';
            }
        }
        $uidArr = $uidArr.')';

        //休息时间字符串数组

        $xxArr = '('."'";

        foreach($xxdataArr as $key=>$val) {

            if($key == count($xxdataArr)-1){
                $xxArr= $xxArr.$val['dt']."'";
            }else{
                $xxArr = $xxArr.$val['dt']."'".','."'";
            }
        }
        $xxArr = $xxArr.')';

        $dailyArr = m('daily')->getall("uid='$this->adminid' and optdt > '2017-08-01' and !(LEFT(optdt,10) in $xxArr)  ",'id,uid,dt,optdt,content ','id desc,uid asc');

        $commentArr =  m('flow_log')->getall("`table`='daily' and optdt > '2017-08-01'and !(left(optdt,10) in $xxArr) AND !ISNULL(`explain`) ",'`explain`,optdt ,mid,checkname,checkid  ','optdt desc');

        $adminArr =  m('admin')->getall("",'`id`,face');

        $lowdailyArr = m('daily')->getall("uid in $uidArr  and optdt > '2017-08-01'and !(LEFT(optdt,10) in $xxArr) ",'id,uid,optdt,content,optname,dt ','`id` desc,uid asc');

        $checkArr =  m('flow_log')->getall("`table`='check' and optdt > '2017-08-01'and !(left(optdt,10) in $xxArr)   GROUP BY mid,checkid",'mid,checkname','optdt desc');


       //print_r($checkArr);die();

        if(!file_exists($yyurl))$yyurl='';
        $yyurljs 	= ''.P.'/we/ying/yingyong/'.$num.'.js';
        if(!file_exists($yyurljs))$yyurljs='';
        $this->assign('arr', $rs);

        $this->assign('dailyArr', $dailyArr);
        $this->assign('checkArr', $checkArr);
        $this->assign('lowidArr', $lowidArr);
        $this->assign('adminArr', $adminArr);
        $this->assign('commentArr', $commentArr);
        $this->assign('xxdataArr', $xxdataArr);
        $this->assign('lowdailyArr', $lowdailyArr);
        $this->assign('openfrom', $this->get('openfrom'));
        $this->assign('yyurl', $yyurl);
        $this->assign('yyurljs', $yyurljs);
    }


    public function commentAction() {


        if ( isajax()){

            $content = $_POST["comment"];

            $mid = $_POST["mid"];

            $time = date('Y-m-d H:i:s');

            m('flow_log')->insert("`table` = 'daily',`mid`='$mid',`status`='0',`name`='点评',`courseid`='0',`optdt`='$time',`explain`='$content',`checkname`='$this->adminname',`checkid`='$this->adminid'");

//          $this->rock->location('?a=detail&m=ying&d=we&num=daily&show=we&mid=26&type=0');

        }

    }

    function lowdailyAction(){


        $xxdataArr = m('kqxxsj')->getall("",'*','`id` desc');

        $lowidArr = m('admin')->getall("superid LIKE '%$this->adminid%'",'*','');
        //休息时间字符串数组

        $xxArr = '('."'";

        foreach($xxdataArr as $key=>$val) {

            if($key == count($xxdataArr)-1){
                $xxArr= $xxArr.$val['dt']."'";
            }else{
                $xxArr = $xxArr.$val['dt']."'".','."'";
            }
        }
        $xxArr = $xxArr.')';


        if ( isajax() ){

            $check = $_POST["check"];

            if($check == '1'){

                $Mid = $_POST["mid"];

                $time = date('Y-m-d H:i:s');

                m('flow_log')->insert("`table` = 'check',`mid`='$Mid',`name`='查看',`optdt`='$time',`checkname`='$this->adminname',`checkid`='$this->adminid'");

            }

            else if($check == '2'){

                $page = $_POST["page"];
                $pagesize = 3;
                $limit= $page*$pagesize-$pagesize;
                $a=array();

                for ($x= $limit; $x<$page*$pagesize; $x++) {

                    $dt = date("Y-m-d",strtotime("-$x day"));
                    array_push($a,$dt);
                }

                $Arr = '('."'";

                foreach($a as $key=>$val) {

                    if($key == count($a)-1){
                        $Arr= $Arr.$val."'";
                    }else{
                        $Arr = $Arr.$val."'".','."'";
                    }
                }
                $Arr = $Arr.')';

                //下属id字符串数组

                $uidArr = '(';

                foreach($lowidArr as $key=>$val) {

                    if($key == count($lowidArr)-1){
                        $uidArr = $uidArr.$val['id'];
                    }else{
                        $uidArr = $uidArr.$val['id'].',';
                    }
                }
                $uidArr = $uidArr.')';


                $lowdailyArr = m('daily')->getall("uid in $uidArr  and (LEFT(optdt,10) in $Arr) and !(LEFT(optdt,10) in $xxArr)  ",'id,uid,optdt,content,optname,dt ','uid asc');

                $this->returnjson($lowdailyArr);

            }


            else{


                $dailyArr = m('daily')->getall("uid='$this->adminid' and optdt > '2017-08-01' and !(LEFT(optdt,10) in $xxArr)  ",'id,uid,dt,optdt,content ','id desc,uid asc');

                $this->returnjson($dailyArr);
            }


        }

        else{


            $commentArr =  m('flow_log')->getall("`table`='daily' and optdt > '2017-08-01'and !(left(optdt,10) in $xxArr) AND !ISNULL(`explain`) ",'`explain`,optdt ,mid,checkname,checkid  ','optdt desc');

            $this->assign('lowidArr', $lowidArr);
            $this->assign('commentArr', $commentArr);
            $this->assign('xxdataArr', $xxdataArr);
            $this->assign('openfrom', $this->get('openfrom'));

        }
    }


    public function writeAction() {


//        m('wxgzh:oauth')->oauthto();  // 静默授权

        $xxdataArr = m('kqxxsj')->getall("",'*','`id` desc');

        $xxArr = '('."'";

        foreach($xxdataArr as $key=>$val) {

            if($key == count($xxdataArr)-1){
                $xxArr= $xxArr.$val['dt']."'";
            }else{
                $xxArr = $xxArr.$val['dt']."'".','."'";
            }
        }
        $xxArr = $xxArr.')';

        $time1 = date('Y-m-d');


        $dailyArr = m('daily')->getall("uid='$this->adminid' and optdt > '2017-08-01' and !(LEFT(optdt,10) in $xxArr)  ",'id,uid,dt,optdt,content ','id desc,uid asc');

        $wqarr	= m('location')->getrows("uid='$this->adminid'  and LEFT(optdt,10) = '$time1' and `type`='1' ",'*','`id` desc');

        $customer	= m('customer')->getrows("uid='$this->adminid' or FIND_IN_SET('$this->adminid',shateid) ",'id,name','`id` desc');

        $this->assign('customer', $customer);

        $this->assign('wqarr', $wqarr);

        $this->assign('xxdataArr', $xxdataArr);

        $this->assign('dailyArr', $dailyArr);

        if ( isajax()){

            $content = $_POST["comment"];

            $da = $_POST["date"];  //判断是写今天还是买明天日报

            $edits = $_POST["edits"];  //判断是否涉及编辑

            $change = $_POST["change"];   //判断切换日期

            if ($change == '1'){

                if($da == 'false'){

                    $dt1 = date("Y-m-d",strtotime("-1 day"));
                    $wqarr	= m('location')->getrows("uid='$this->adminid'  and LEFT(optdt,10) = '$dt1' ",'*','`id` desc');
                    $this->returnjson($wqarr);

                }else{
                    $wqarr	= m('location')->getrows("uid='$this->adminid'  and LEFT(optdt,10) = '$time1' ",'*','`id` desc');
                    $this->returnjson($wqarr);
                }
            }

            if($da == 'true'){

                if ($edits == '1'){

                    $mid = $_POST["mid"];

                    m('daily')->update("`content` = '$content'","`id` = '$mid'");


                }
                else{

                    $time = date('Y-m-d H:i:s');

                    $dt = substr($time,0,10);

                    m('daily')->insert("`uid` = '$this->adminid',`dt`='$dt',`content`='$content',`adddt`='$time',`optdt`='$time',`optname`='$this->adminname'");
                }

            }else{

                $time = date('Y-m-d H:i:s');
                $dt = date("Y-m-d",strtotime("-1 day"));

                m('daily')->insert("`uid` = '$this->adminid',`dt`='$dt',`content`='$content',`adddt`='$time',`optdt`='$time',`optname`='$this->adminname'");

            }
        }

    }



    public function locationAction()
	{
		$this->title = '考勤定位';
		$dt 	= $this->rock->date;
		$dwarr	= m('location')->getrows("uid='$this->adminid' and `optdt` like '$dt%' and type = '0'",'*','`id` desc');

		$this->assign('dwarr', $dwarr);
		$kqrs 	= m('kaoqin')->dwdkrs($this->adminid, $this->date);
		$isgzh	= m('wxgzh:index')->isusegzh();
		$this->assign('isgzh', $isgzh);
		$this->assign('kqrs', $kqrs);
		$dwids	= arrvalue($kqrs, 'dwids');
		$kqors	= array();
		if(!isempt($dwids)){
			$kqors = m('kqdw')->getrows("id in($dwids) and `id`<>".$kqrs['id']."");
		}
		$this->assign('kqors', $kqors);
	}
}